// services/authService.js
import api from './api';

export const authService = {
    // 用户登录
    login: async (credentials) => {
        try {
            //console.log('登录请求数据:', credentials);
            const response = await api.post('/api/auth/login', credentials);
            return response;
        } catch (error) {
            //console.error('登录服务错误:', error);
            throw error;
        }
    },

    // 用户注册
    register: async (userData) => {
        try {
            //console.log('注册请求数据:', userData);
            // 移除确认密码字段
            const { confirmPassword, ...registerData } = userData;
            const response = await api.post('/api/auth/register', registerData);
            return response;
        } catch (error) {
            //console.error('注册服务错误:', error);
            throw error;
        }
    },

    // 用户登出
    logout: async () => {
        try {
            const response = await api.post('/api/auth/logout');
            localStorage.removeItem('token');
            localStorage.removeItem('user');
            return response;
        } catch (error) {
            //console.error('登出服务错误:', error);
            throw error;
        }
    },

    // 刷新token
    refreshToken: async () => {
        try {
            const response = await api.post('/api/auth/refresh');
            return response;
        } catch (error) {
            console.error('刷新token错误:', error);
            throw error;
        }
    },

    // 获取当前用户信息
    getCurrentUser: async () => {
        try {
            const response = await api.get('/api/auth/current-user');
            return response;
        } catch (error) {
            console.error('获取用户信息错误:', error);
            throw error;
        }
    }
};
